JavaScript syntax
part 18/59 · 107.4 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
However, JavaScript strings are immutable:
greeting[0] = "H"; // Fails.
Applying the equality operator ("==") to two strings returns true, if the strings have the same contents, which means: of the same length and containing the same sequence of characters (case is significant for alphabets). Thus:
const x = "World";
const compare1 = ("Hello, " + x == "Hello, World"); // Here compare1 contains true.
const compare2 = ("Hello, " + x == "hello, World"); // Here compare2 contains ...
// ... false since the ...
// ... first characters ...
// ... of both operands ...
// ... are not of the same case.
Quotes of the same type cannot be nested unless they are escaped.
let x = '"Hello, World!" he said.'; // Just fine.
x = ""Hello, World!" he said."; // Not good.
x = "\"Hello, World!\" he said."; // Works by escaping " with \"
The String constructor creates a string object (an object wrapping a string):
const greeting = new String("Hello, World!");
These objects have a valueOf method returning the primitive string wrapped within them:
const s = new String("Hello !");
typeof s; // Is 'object'.
typeof s.valueOf(); // Is 'string'.
Equality between two String objects does not behave as with string primitives:
const s1 = new String("Hello !");
const s2 = new String("Hello !");
s1 == s2; // Is false, because they are two distinct objects.
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────